package com.example.mall_system.controller;

import com.example.mall_system.common.Result;
import com.example.mall_system.entity.CartItem;
import com.example.mall_system.entity.Product;
import com.example.mall_system.mapper.CartItemMapper;
import com.example.mall_system.mapper.ProductMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/test")
public class TestController {

    @Autowired
    private CartItemMapper cartItemMapper;

    @Autowired
    private ProductMapper productMapper;

    @GetMapping("/cart-items/{userId}")
    public Result<?> getCartItems(@PathVariable Long userId) {
        try {
            List<CartItem> cartItems = cartItemMapper.findByUserId(userId);
            return Result.success(cartItems);
        } catch (Exception e) {
            return Result.error(500, "获取购物车失败: " + e.getMessage());
        }
    }

    @GetMapping("/products")
    public Result<?> getProducts() {
        try {
            List<Product> products = productMapper.findAllProducts();
            return Result.success(products);
        } catch (Exception e) {
            return Result.error(500, "获取商品失败: " + e.getMessage());
        }
    }

    @GetMapping("/cart-item/{id}")
    public Result<?> getCartItem(@PathVariable Integer id) {
        try {
            CartItem cartItem = cartItemMapper.findById(id);
            return Result.success(cartItem);
        } catch (Exception e) {
            return Result.error(500, "获取购物车项失败: " + e.getMessage());
        }
    }
} 